package com.jianggujin.redis.panel.value;

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;

import javax.swing.JComboBox;
import javax.swing.JPanel;

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rtextarea.RTextScrollPane;

import com.jianggujin.redis.node.RedisKey;
import com.jianggujin.redis.panel.ValuePanel;

public class StringValuePanel extends JPanel implements ValuePanel {
    private RSyntaxTextArea valueArea;
    private JComboBox<String> showTypeComBox;

    public StringValuePanel() {
        super(new BorderLayout());
        showTypeComBox = new JComboBox<>(new String[] { "Raw", "JSON", "XML" });
        showTypeComBox.addItemListener(e -> {
            if (ItemEvent.SELECTED == e.getStateChange()) {
                Object item = e.getItem();
                if ("Raw".equals(item)) {
                    valueArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);
                } else if ("JSON".equals(item)) {
                    valueArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSON);
                } else if ("XML".equals(item)) {
                    valueArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML);
                }
            }
        });
        add(showTypeComBox, BorderLayout.NORTH);

        valueArea = new RSyntaxTextArea();
        valueArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);
        valueArea.setCodeFoldingEnabled(true);
        RTextScrollPane sp = new RTextScrollPane(valueArea);
        sp.setLineNumbersEnabled(true);
        add(sp);
    }

    @Override
    public void showValue(RedisKey redisKey) {
        String value = redisKey.getClient().get(redisKey.getKey());
        valueArea.setText(value == null ? "" : value);
    }

}
